package org.vincen.helloworld.bonecp;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.vincen.helloworld.jdbc.CRUD;
import org.vincen.helloworld.jdbc.ReadProperties;

import com.jolbox.bonecp.BoneCPDataSource;

public class PractiseBonecp {

	public static void main(String[] args) {
		
		// create a new datasource object
	 	BoneCPDataSource ds = new BoneCPDataSource();
	 	
	 	//get the db connection information
	 	ReadProperties rp = new ReadProperties();
	 	String[] dbinfo = new String[3];
		try {
			dbinfo = rp.getProperties();
		} catch (IOException e) {
			e.printStackTrace();
		}
	 	ds.setDefaultAutoCommit(false);
	 	ds.setJdbcUrl(dbinfo[0]);		// set the JDBC url
		ds.setUsername(dbinfo[1]);		// set the username
		ds.setPassword(dbinfo[2]);		// set the password
		
		// fetch a connection
		Connection connection = null;
		try {
			connection = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		} 		
		
		//crud
		CRUD crud = new CRUD();
		try {
			crud.read(connection);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		// close the connection
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}				
		ds.close();					// close the datasource pool


	}

}
